<?php
namespace app\wenchuang\controller;

use app\wenchuang\model\NavModel;
use app\wenchuang\validate\NavValidate;
use cmf\controller\AdminBaseController;
use tree\Tree;

class NavController extends AdminBaseController
{
    public function index()
    {
        // 树形组件
        $navModel = new NavModel();
        $data = $navModel->order('list_order', 'desc')->select()->toArray();
        $this->assign('nav', $data);
        return $this->fetch();
    }
    public function listOrder()
    {
        parent::listOrders(new NavModel());
        $this->success('排序成功');
    }
    public function add()
    {
        // 查询现有的导航菜单
        $navModel = new NavModel();
        $data = $navModel->order(['list_order' => 'desc'])->select()->toArray();
        // 处理成树形菜单
        $tree = new Tree();
        $str = "<option value='\$id' \$selected>\$spacer \$name</option>";
        $tree->init($data);
        $data = $tree->getTree(0, $str);
        $this->assign('nav', $data);
        return $this->fetch();
    }
    public function addPost()
    {
        $data = input('post.');
        // 验证数据
        $validate = new NavValidate();
        $result = $validate->scene('add')->check($data);
        if ($result !== true) {
            $this->error($validate->getError());
        }
        // 存库
        $navModel = New NavModel();
        if ($navModel->save($data)) {
            $this->success('添加成功', 'Nav/index');
        }
        $this->error('添加失败');
    }
    public function delete()
    {
        $id = input('id');
        // 查询是否有子菜单，有则不允许删除
        $navModel = New NavModel();
        $childNav = $navModel->where('parent_id', '=', $id)->select();
        if (count($childNav) > 0) {
            $this->error('该菜单下存在子菜单，无法删除！');
        }
        $delete = $navModel->where('id', '=', $id)->delete();
        if ($delete) {
            $this->success('删除成功');
        }
        $this->error('删除失败');
    }
    public function delete2()
    {
        $id = input('id');
        // 查询是否有子菜单，有则连同子菜单一起删除
        $navModel = new NavModel();
        $childNav = $navModel->where('parent_id', '=', $id)->select();
        if (count($childNav) > 0) {
            $navModel->where('parent_id', '=', $id)->delete();
        }
        $delete = $navModel->where('id', '=', $id)->delete();
        if ($delete) {
            $this->success('删除成功');
        }
        $this->error('删除失败');
    }
    public function softDelete()
    {
        $id = input('id');
        $navModel = New NavModel();
        // 查询要删除的数据
        $nav = $navModel->where('id', '=', $id)->find();
        if ($nav->delete()) {
            $this->success('删除成功');
        }
        $this->error('删除失败');
    }
    public function edit()
    {
        $id = input('id');
        $navModel = New NavModel();
        $nav = $navModel->where('id', '=', $id)->find();
        $data = $navModel->order(['list_order' => 'desc'])->select()->toArray();
        // 处理成树形菜单
        $tree = new Tree();
        $str = "<option value='\$id' \$selected>\$spacer \$name</option>";
        $tree->init($data);
        $data = $tree->getTree(0, $str, $nav['parent_id']);
        $this->assign('nav', $nav);
        $this->assign('all_nav', $data);
        return $this->fetch();
    }
    public function editPost()
    {
        $data = input('post.');
        // 验证数据
        $validate = new NavValidate();
        $result = $validate->scene('edit')->check($data);
        if ($result !== true) {
            $this->error($validate->getError());
        }
        // 判断上下级
        if ($data['parent_id'] == $data['id']) {
            $this->error('不能选择当前菜单作为上级菜单');
        }
        // 保存数据
        $navModel = New NavModel();
        $nav = $navModel->where('id', '=', $data['id'])->find();
        $nav->name = $data['name'];
        $nav->url = $data['url'];
        $nav->parent_id = $data['parent_id'];
        if ($nav->save()) {
            $this->success('保存成功');
        }
        $this->error('保存失败');
    }
}
